package com.smec.moblie.activity;

import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.smec.moblie.R;
import com.smec.moblie.adapter.RemesNoticeAdapter;
import com.smec.moblie.bean.MaintenanceNoticeBean;
import com.smec.moblie.control.MainControl;
import com.smec.moblie.crash.ExitAppUtils;

import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.util.ArrayList;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 广播通知列表
 * Created by sunyubo on 16/2/26.
 */
public class RemesNoticeListActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {

    /** 返回按钮 **/
    @BindView(R.id.imageview_back)
    ImageView imageviewBack;
    /** 广播通知listview **/
    @BindView(R.id.listView_remesNotice)
    ListView listView_RemesNotice;
    /** 下拉刷新控件 **/
    @BindView(R.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;
    /** 无数据时显示文字 **/
    @BindView(R.id.textView_noData)
    TextView textView_noData;
    @BindView(R.id.rl_title)
    RelativeLayout rl_title;

    private RemesNoticeAdapter remesNoticeAdapter;

    private ArrayList<MaintenanceNoticeBean> maintenanceNoticeBeanList = new ArrayList<MaintenanceNoticeBean>();

    @Override
    protected void setContentView() {
        setContentView(R.layout.remes_act_remesnoticelist);
    }

    @Override
    protected void initView() {
        ExitAppUtils.getInstance().addActivity(this);

//        PublicControl.setMargins(rl_title, 0, getStatusBarHeight(RemesNoticeListActivity.this), 0, 0);

        ButterKnife.bind(this);
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setColorSchemeColors(Color.GREEN, Color.BLUE, Color.YELLOW);

        remesNoticeAdapter = new RemesNoticeAdapter(RemesNoticeListActivity.this);
        listView_RemesNotice.setAdapter(remesNoticeAdapter);
    }

    @Override
    protected void getData() {
        refreshLayout.post(new Runnable() {
            @Override
            public void run() {
                refreshLayout.setRefreshing(true);
            }
        });

        new GetRemesNoticeAsyncTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onRefresh() {
        refreshLayout.post(new Runnable() {
            @Override
            public void run() {
                refreshLayout.setRefreshing(true);
            }
        });

        new GetRemesNoticeAsyncTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    }

    @OnClick(R.id.imageview_back)
    public void imageview_back() {
        RemesNoticeListActivity.this.finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    private class GetRemesNoticeAsyncTask extends AsyncTask<Void, Void, Void> {
        boolean isok = true;

        @Override
        protected Void doInBackground(Void... params) {
            try {
                maintenanceNoticeBeanList = MainControl.getMaintenanceNoticeByUserId(MainActivity.USERNAME);
            } catch (IOException e) {
                e.printStackTrace();
                isok = false;
            } catch (XmlPullParserException e) {
                e.printStackTrace();
                isok = false;
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            if(isok) {
                remesNoticeAdapter.setList(maintenanceNoticeBeanList);
                if (maintenanceNoticeBeanList.size() > 0) {
                    textView_noData.setVisibility(View.GONE);
                } else {
                    textView_noData.setVisibility(View.VISIBLE);
                }
            }else {
                Toast.makeText(RemesNoticeListActivity.this, "获取数据失败，请检查网络", Toast.LENGTH_SHORT).show();
            }
            refreshLayout.post(new Runnable() {
                @Override
                public void run() {
                    refreshLayout.setRefreshing(false);
                }
            });

            super.onPostExecute(aVoid);
        }
    }
}
